package com.version1_3.service.implement;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.util.format.MapToJson;
import com.util.format.TimeFormat;
import com.util.property.Properties;
import com.version1_3.dao.PageDao1_3;
import com.version1_3.model.MainPageModel;
import com.version1_3.service.PageService1_3;
import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by reeves on 2015/12/23.
 */

@Service
public class PageServiceImpl13 implements PageService1_3 {
    private static final Logger LOGGER = Logger.getLogger(PageServiceImpl13.class);

    @Autowired
    private PageDao1_3 pageDao13;


    // 1. 获取登陆用户主界面信息
    @Override
    @Cacheable(value = {"loginMainPageVersion1_3"})
    @CacheEvict(value = "loginMainPageVersion1_3", condition = "#cacheTag==true", allEntries = true)
    public String getMainPage(int firstType, int userId, int pageNumber, boolean cacheTag) {
        MainPageModel mainPageModel = pageDao13.getMainPage(firstType, userId, pageNumber);
        if (mainPageModel == null) {
            return "{\"code\":500}";
        }

        /**
         * 这里是mainPageModel中对于图片路径和其他数据的进一步处理
         * 使用阿里云OSS
         */
        if (mainPageModel.getPageList() != null) {
            for (Map pageMap : mainPageModel.getPageList()) {
                pageMap.replace("imageSrc", pageMap.get("imageSrc") + Properties.ImageQuality);
                pageMap.replace("pageDate", TimeFormat.convertTimeToFormat((Date) pageMap.get("pageDate")));
            }
        }

        if (mainPageModel.getTitleList() != null) {
            for (Map titleMap : mainPageModel.getTitleList()) {
                titleMap.replace("imageSrc", titleMap.get("imageSrc") + Properties.ImageQuality);
            }
        }

        return mainPageModel.jsonSerialize();
    }

    // 2. 获取未登陆用户主界面信息
    @Override
    @Cacheable(value = {"noLoginMainPageVersion1_3"})
    @CacheEvict(value = "noLoginMainPageVersion1_3", condition = "#cacheTag==true", allEntries = true)
    public String getMainPageNoLogin(JSONArray firstFocusJsonArray, int firstType, int pageNumber, boolean cacheTag) {
        MainPageModel mainPageModel = pageDao13.getMainPageNoLogin(firstFocusJsonArray, firstType, pageNumber);

        /**
         * 这里是mainPageModel中可以对数据进行的进一步处理
         * 使用阿里云OSS
         */
        if (mainPageModel == null) {
            return "{\"code\":500}";
        }

        for (Map pageMap : mainPageModel.getPageList()) {
            pageMap.replace("imageSrc", pageMap.get("imageSrc") + Properties.ImageQuality);
            pageMap.replace("pageDate", TimeFormat.convertTimeToFormat((Date) pageMap.get("pageDate")));
        }

        if (mainPageModel.getTitleList() != null) {
            for (Map titleMap : mainPageModel.getTitleList()) {
                titleMap.replace("imageSrc", titleMap.get("imageSrc") + Properties.ImageQuality);
            }
        }

        return mainPageModel.jsonSerialize();
    }

    // 3. 获取三级界面信息
    @Override
    @Cacheable(value = {"thirdTypePageCacheVersion1_3"})
    @CacheEvict(value = "thirdTypePageCacheVersion1_3", condition = "#cleanCache==true", allEntries = true)
    public String getThirdPage(int thirdTypeId, int pageNumber, boolean cleanCache) {
        List pageList = pageDao13.getThirdPageList(thirdTypeId, pageNumber);
        boolean isLast = false;
        Map<String, Object> getThirdPageMap = new HashMap<>();

        if (pageList == null || pageList.isEmpty()) {
            isLast = true;
        } else {
            for (int i = 0; i < pageList.size(); i++) {
                Map pageMap = (Map) pageList.get(i);
                pageMap.replace("imageSrc", (String) pageMap.get("imageSrc") + Properties.ImageQuality);
            }
        }

        // 请求第一页数据时会加入三级分类的描述信息
        if (pageNumber == 1) {
            String thirdTypeDescription = pageDao13.getThridTypeDescription(thirdTypeId);
            getThirdPageMap.put("description", thirdTypeDescription);
        }

        getThirdPageMap.put("isLast", isLast);
        getThirdPageMap.put("result", pageList);
        return JSON.toJSONString(getThirdPageMap);
    }

    /**
     * @Description: 获取除正文之外的其他信息
     */
    @Override
    public String getPageOtherInfo(int userId, int pageId) {
        Map<String, Object> pageOtherInfoMap = pageDao13.getPageOtherInfo(userId, pageId);
        List<Map<String, Object>> recomArtcList = (List<Map<String, Object>>) pageOtherInfoMap.get("recomArtc"); //推荐文章数组
        List<Map<String, Object>> comFirPageList = (List<Map<String, Object>>) pageOtherInfoMap.get("comFirPage"); //第一页评论列表

        Map<String, Object> getPageOtherInfoMap = new HashMap<>();

        /**
         * 推荐文章标题图片URL处理
         */
        for (Map tempMap : recomArtcList) {
            tempMap.replace("imageSrc", (String) tempMap.get("imageSrc") + Properties.ImageQuality);
            tempMap.replace("pageDate", TimeFormat.convertTimeToFormat((Date) tempMap.get("pageDate")));
        }

        /**
         * 评论列表用户头像URL处理
         */
        for (Map tempMap : comFirPageList) {
            String userPhoto = (String) tempMap.get("userPhoto");
            tempMap.replace("userPhoto", userPhoto + Properties.ImageLowQuality);
        }

        getPageOtherInfoMap.put("recomArtc", recomArtcList);
        getPageOtherInfoMap.put("comFirPage", comFirPageList);
        getPageOtherInfoMap.put("praNum", (int) pageOtherInfoMap.get("praNum"));
        getPageOtherInfoMap.put("criNum", (int) pageOtherInfoMap.get("criNum"));
        getPageOtherInfoMap.put("userChoice", (int) pageOtherInfoMap.get("userChoice"));
        getPageOtherInfoMap.put("typeBgImgSrc", (String) pageOtherInfoMap.get("typeBgImgSrc"));
        getPageOtherInfoMap.put("isSubscri", (boolean) pageOtherInfoMap.get("isSubscri"));
        getPageOtherInfoMap.put("comNum", (int) pageOtherInfoMap.get("comNum"));
        return JSON.toJSONString(getPageOtherInfoMap);
    }

    /**
     * 配合诸葛IO-获取文章相关信息
     * @param pageId
     * @return
     */
    @Override
    @Cacheable(value = {"articleDetailCacheVersion1_3"})
    public String getArticleDetail(int pageId) {

        Map<String, Object> articleDetailMap = pageDao13.getArticleDetail(pageId);

        if (articleDetailMap != null) {
            return JSONObject.toJSONString(articleDetailMap);
        } else {
            return "";
        }
    }

    /**
     * 获取推送文章信息
     *
     * @param key
     * @return
     */
    @Override
    @Cacheable(value = {"pushArticleCacheVersion1_3"})
    public String getPushArticleInfo(String key) {
        List<Map<String, Object>> pushArticleList = pageDao13.getPushArticleInfo(key);

        return new MapToJson("pushArticle", pushArticleList).toJsonString();
    }
}
